package com.molichuxing.gateway.bff.order.convertor;

import com.molichuxing.framework.utils.DateUtil;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.gateway.bff.order.entity.request.create.PaymentOfflineCreateVo;
import com.molichuxing.gateway.bff.order.entity.request.create.PaymentOfflineScheduleCreateVo;
import com.molichuxing.gateway.bff.order.entity.response.*;
import com.molichuxing.services.business.dto.request.create.OfflineBizCreateDto;
import com.molichuxing.services.business.dto.request.create.OfflineOrdersBizCreateDto;
import com.molichuxing.services.business.dto.response.*;
import com.molichuxing.services.contants.OrderStatusConst;
import com.molichuxing.services.infrastructure.dto.response.*;
import com.molichuxing.services.property.OrderExclusivelStatusEnum;
import com.molichuxing.services.property.OrderScheduleStatusEnum;
import com.molichuxing.services.property.OrderSubscribeStatusEnum;
import com.molichuxing.services.property.OrderTypeEnum;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class Convert {
	// 里程单位
	public static final String UNIT_MILEAGE = "公里";
	// 金额单位
	public static final String UNIT_MONEY = "元";

	public static OrderSubscribeVo toOrderSubscribeVo(OrderSubscribeBizDto beanDto) {
		OrderSubscribeVo beanVo = null;
		if (beanDto != null) {
			beanVo = new OrderSubscribeVo();
			BeanUtils.copyProperties(beanDto, beanVo);
			Integer totalMileage = beanDto.getBaseMileage() + beanDto.getPackageMileage() + beanDto.getGiftMileage();
			BigDecimal totalAmount = beanDto.getCartFee().add(beanDto.getDeposit());
			// 里程详情
			List<ParamDetailVo> mileageDetail = new ArrayList<>();
			mileageDetail.add(new ParamDetailVo("基础里程", beanDto.getBaseMileage() + UNIT_MILEAGE));
			mileageDetail.add(new ParamDetailVo("订阅里程", beanDto.getPackageMileage() + UNIT_MILEAGE));
			if (beanDto.getGiftMileage() != null && beanDto.getGiftMileage() > 0) {
				mileageDetail.add(new ParamDetailVo("赠送里程", beanDto.getGiftMileage() + UNIT_MILEAGE));
			}
			// 金额详情
			List<ParamDetailVo> amountDetail = new ArrayList<>();
			amountDetail.add(
					new ParamDetailVo("租车费", beanDto.getRental().stripTrailingZeros().toPlainString() + UNIT_MONEY));
			amountDetail.add(new ParamDetailVo("基础里程费",
					beanDto.getBaseMileagePrice().stripTrailingZeros().toPlainString() + UNIT_MONEY));
			amountDetail.add(new ParamDetailVo("订阅里程费",
					beanDto.getPackageMileagePrice().stripTrailingZeros().toPlainString() + UNIT_MONEY));

			if (beanDto.getDeposit() != null && beanDto.getDeposit().compareTo(BigDecimal.ZERO) > 0) {
				amountDetail.add(new ParamDetailVo("保证金",
						beanDto.getDeposit().stripTrailingZeros().toPlainString() + UNIT_MONEY));
			}

			if (beanDto.getPurchaseMileage() != null && beanDto.getPurchaseMileage() > 0
					&& beanDto.getPurchaseFee() != null) {
				totalMileage = totalMileage + beanDto.getPurchaseMileage();
				totalAmount = totalAmount.add(beanDto.getPurchaseFee());
				mileageDetail.add(new ParamDetailVo("加购里程", beanDto.getPurchaseMileage() + UNIT_MILEAGE));
				amountDetail.add(new ParamDetailVo("加购里程费",
						beanDto.getPurchaseFee().stripTrailingZeros().toPlainString() + UNIT_MONEY));
			}
			if (beanDto.getBeyondMileage() != null && beanDto.getBeyondMileage() > 0
					&& beanDto.getBeyondFee() != null) {
				totalMileage = totalMileage + beanDto.getBeyondMileage();
				totalAmount = totalAmount.add(beanDto.getBeyondFee());
				mileageDetail.add(new ParamDetailVo("已结算超里程", beanDto.getBeyondMileage() + UNIT_MILEAGE));
				amountDetail.add(new ParamDetailVo("已结算超里程费",
						beanDto.getBeyondFee().stripTrailingZeros().toPlainString() + UNIT_MONEY));
			}
			// 总里程(基础里程+订阅里程+赠送里程+加购里程+已结算超里程)
			beanVo.setTotalMileage(totalMileage);
			// 订单总金额(租车费+基础里程费+订阅里程费+加购里程费+已结算超里程费)
			beanVo.setTotalAmount(totalAmount);
			// 里程详情
			beanVo.setMileageDetail(mileageDetail);
			// 金额详情
			beanVo.setAmountDetail(amountDetail);

			// 审核状态
			if (OrderSubscribeStatusEnum.TRADE_UNAUDIT.equals(beanDto.getOrderStatus())) {
				// 待审核
				beanVo.setAuditStatusStr(OrderSubscribeStatusEnum.TRADE_UNAUDIT.getName());
			} else if (OrderStatusConst.CREDIT_PASS_SUBSCRIBE_STATUS.contains(beanDto.getOrderStatus())) {
				// 审核通过
				beanVo.setAuditStatusStr(OrderSubscribeStatusEnum.TRADE_PASS.getName());
			} else if (OrderStatusConst.CREDIT_REJECT_SUBSCRIBE_STATUS.contains(beanDto.getOrderStatus())) {
				// 审核驳回
				beanVo.setAuditStatusStr(OrderSubscribeStatusEnum.TRADE_REJECTED.getName());
			}

			// 退款状态
			if (OrderSubscribeStatusEnum.TRADE_REJECTED.equals(beanDto.getOrderStatus())) {
				// 待处理
				beanVo.setRefundStatusStr("待处理");
			} else if (OrderSubscribeStatusEnum.TRADE_REFUND.equals(beanDto.getOrderStatus())) {
				// 退款中
				beanVo.setRefundStatusStr("退款中");
			} else if (OrderSubscribeStatusEnum.TRADE_FINISH.equals(beanDto.getOrderStatus())) {
				// 已退款
				beanVo.setRefundStatusStr("已退款");
			}
		}
		return beanVo;
	}

	public static List<OrderTradeFlowVo> toOrderTradeFlowVo(List<OrderTradeFlowBizDto> beanDtoList) {
		List<OrderTradeFlowVo> beanVoList = null;
		if (beanDtoList != null && !beanDtoList.isEmpty()) {
			beanVoList = new ArrayList<>();
			for (OrderTradeFlowBizDto beanDto : beanDtoList) {
				beanVoList.add(toOrderTradeFlowVo(beanDto));
			}
		}
		return beanVoList;
	}

	public static OrderTradeFlowVo toOrderTradeFlowVo(OrderTradeFlowBizDto beanDto) {
		OrderTradeFlowVo beanVo = null;
		if (beanDto != null) {
			beanVo = new OrderTradeFlowVo();
			BeanUtils.copyProperties(beanDto, beanVo);
		}

		return beanVo;
	}

	public static OrderSubscribeBaseVo toOrderSubscribeBaseVo(OrderSubscribeBizDto beanDto) {
		OrderSubscribeBaseVo beanVo = null;
		if (beanDto != null) {
			beanVo = new OrderSubscribeBaseVo();
			BeanUtils.copyProperties(beanDto, beanVo);

			Integer totalMileage = beanDto.getBaseMileage() + beanDto.getPackageMileage() + beanDto.getGiftMileage();
			BigDecimal totalAmount = beanDto.getCartFee().add(beanDto.getDeposit());

			if (beanDto.getPurchaseMileage() != null && beanDto.getPurchaseMileage() > 0) {
				totalMileage = totalMileage + beanDto.getPurchaseMileage();
				totalAmount = totalAmount.add(beanDto.getPurchaseFee());
			}
			if (beanDto.getBeyondMileage() != null && beanDto.getBeyondMileage() > 0) {
				totalMileage = totalMileage + beanDto.getBeyondMileage();
				totalAmount = totalAmount.add(beanDto.getBeyondFee());
			}
			// 总里程(基础里程+订阅里程+赠送里程+加购里程+已结算超里程)
			beanVo.setTotalMileage(totalMileage);
			// 订单总金额(租车费+基础里程费+订阅里程费+加购里程费+已结算超里程费+保证金)
			beanVo.setTotalAmount(totalAmount);

			// 是否续订订单
			if (beanDto.getParentOrderCode() == null || beanDto.getParentOrderCode() == 0L) {
				beanVo.setIsRenew(false);
			} else {
				beanVo.setIsRenew(true);
			}
		}
		return beanVo;
	}

	public static PaymentOfflineScheduleVo toOrderScheduleOfflineVo(OrderScheduleOfflineBizDto beanDto) {
		PaymentOfflineScheduleVo beanVo = null;
		if (beanDto != null) {
			beanVo = new PaymentOfflineScheduleVo();
			BeanUtils.copyProperties(beanDto, beanVo);
		}

		return beanVo;
	}

	public static OrderUsedCarVo toOrderUsedCarVo(OrderUsedCarBizDto beanDto) {
		OrderUsedCarVo beanVo = null;
		if (beanDto != null) {
			beanVo = new OrderUsedCarVo();
			BeanUtils.copyProperties(beanDto, beanVo);
			beanVo.setPurchaseMileage(beanDto.getReletMileage());
			beanVo.setBeyondMileage(beanDto.getBeyondMileage() == null ? 0 : beanDto.getBeyondMileage());
		}

		return beanVo;
	}

	public static OrderExclusivelVo toOrderExclusivelVo(OrderExclusivelBizDto beanDto) {
		OrderExclusivelVo beanVo = null;
		if (beanDto != null) {
			beanVo = new OrderExclusivelVo();
			BeanUtils.copyProperties(beanDto, beanVo);
			if (beanDto.getCarId() == null || beanDto.getCarId() <= 0) {
				beanVo.setIsCar(false);
			} else {
				beanVo.setIsCar(true);
			}
			// 付款方式详情
			List<ParamDetailVo> paymentMethodDetail = new ArrayList<>();
			paymentMethodDetail.add(new ParamDetailVo("还款周期", beanDto.getRepaymentPeriod() + "个月"));
			paymentMethodDetail.add(new ParamDetailVo("首付期数", beanDto.getFirstTerms() + "期"));
			paymentMethodDetail.add(new ParamDetailVo("结算日", beanDto.getSettleDay() + "号"));
			beanVo.setPaymentMethodDetail(paymentMethodDetail);
			// 订单总金额
			beanVo.setTotalAmount(beanDto.getCartFee().add(beanDto.getDeposit()));

			// 审核状态
			if (OrderExclusivelStatusEnum.TRADE_UNAUDIT.equals(beanDto.getOrderStatus())
					|| OrderExclusivelStatusEnum.TRADE_CREDIT_UNAUDIT.equals(beanDto.getOrderStatus())) {
				// 待审核
				beanVo.setAuditStatusStr(OrderExclusivelStatusEnum.TRADE_UNAUDIT.getName());
			} else if (OrderStatusConst.CREDIT_PASS_EXCLUSIVEL_STATUS.contains(beanDto.getOrderStatus())) {
				// 审核通过
				beanVo.setAuditStatusStr(OrderExclusivelStatusEnum.TRADE_PASS.getName());
			} else if (OrderStatusConst.CREDIT_REJECT_EXCLUSIVEL_STATUS.contains(beanDto.getOrderStatus())) {
				// 审核驳回
				beanVo.setAuditStatusStr(OrderExclusivelStatusEnum.TRADE_REJECTED.getName());
			}

			// 退款状态
			if (OrderExclusivelStatusEnum.TRADE_REJECTED.equals(beanDto.getOrderStatus())) {
				// 待处理
				beanVo.setRefundStatusStr("待处理");
			} else if (OrderExclusivelStatusEnum.TRADE_REFUND.equals(beanDto.getOrderStatus())) {
				// 退款中
				beanVo.setRefundStatusStr("退款中");
			} else if (OrderExclusivelStatusEnum.TRADE_FINISH.equals(beanDto.getOrderStatus())) {
				// 已退款
				beanVo.setRefundStatusStr("已退款");
			}
		}
		return beanVo;
	}

	public static OrderExclusivelBaseVo toOrderExclusivelBaseVo(OrderExclusivelDto beanDto) {
		OrderExclusivelBaseVo beanVo = null;
		if (beanDto != null) {
			beanVo = new OrderExclusivelBaseVo();
			BeanUtils.copyProperties(beanDto, beanVo);
			beanVo.setTotalAmount(beanDto.getCartFee().add(beanDto.getDeposit()));
			// 还款总期数(用车时长/还款周期)
			if (beanDto.getTransferTime() != null && beanDto.getRepaymentPeriod() != null
					&& beanDto.getRepaymentPeriod() > 0) {
				beanVo.setTotalPeriods((int) Math.ceil(beanDto.getTransferTime() / beanDto.getRepaymentPeriod()));
			}
		}

		return beanVo;
	}

	public static OrderRepaymentDetailVo toOrderRepaymentDetailVo(OrderScheduleDto beanDto) {
		OrderRepaymentDetailVo beanVo = null;
		if (beanDto != null) {
			beanVo = new OrderRepaymentDetailVo();
			BeanUtils.copyProperties(beanDto, beanVo);
			beanVo.setPeriodsStr(beanDto.getPeriods() + "期");
			if (OrderScheduleStatusEnum.ALREADY.equals(beanDto.getStatus())) {
				// 已还款，判断逾期时间为支付时间
				long overdueDay = DateUtil.getDaysDiff(beanDto.getPayableDate(), beanDto.getPaySuccessTime());
				if (overdueDay > 0L) {
					// 逾期
					beanVo.setIsOverdue(true);
					beanVo.setIsOverdueStr("是");
					beanVo.setOverdueDays((int) overdueDay);
					beanVo.setOverdueDaysStr(beanVo.getOverdueDays() + "天");
				} else {
					// 未逾期
					beanVo.setIsOverdue(false);
					beanVo.setIsOverdueStr("否");
					beanVo.setOverdueDays(0);
					beanVo.setOverdueDaysStr("--");
				}
			} else {
				// 未还款，判断逾期时间为当前时间
				long overdueDay = DateUtil.getDaysDiff(beanDto.getPayableDate(), LocalDateTime.now());
				if (overdueDay > 0) {
					// 逾期
					beanVo.setIsOverdue(true);
					beanVo.setIsOverdueStr("是");
					beanVo.setOverdueDays((int) overdueDay);
					beanVo.setOverdueDaysStr(beanVo.getOverdueDays() + "天");
				} else {
					// 未逾期
					beanVo.setIsOverdue(false);
					beanVo.setIsOverdueStr("--");
					beanVo.setOverdueDays(0);
					beanVo.setOverdueDaysStr("--");
				}
			}
		}
		return beanVo;
	}

	public static OfflineBizCreateDto toOfflineBizCreateDto(PaymentOfflineCreateVo createVo) {
		OfflineBizCreateDto createDto = new OfflineBizCreateDto();
		BeanUtils.copyProperties(createVo, createDto);
		createDto.setPayTime(ObjectCastUtil.castLocalDateTime(createVo.getPayTime()));
		createDto.setVoucherImgList(createVo.getVoucherImgs());
		List<OfflineOrdersBizCreateDto> offlineOrdersList = new ArrayList<>();
		OfflineOrdersBizCreateDto offlineOrdersBizCreateDto = null;
		for (PaymentOfflineScheduleCreateVo offlineOrdersBizCreateVo : createVo.getOfflineOrdersList()) {
			offlineOrdersBizCreateDto = new OfflineOrdersBizCreateDto();
			BeanUtils.copyProperties(offlineOrdersBizCreateVo, offlineOrdersBizCreateDto);
			offlineOrdersList.add(offlineOrdersBizCreateDto);
		}
		createDto.setOfflineOrdersList(offlineOrdersList);

		return createDto;
	}

	public static UserOrderVo toUserOrderVo(UserOrderBizDto beanDto) {
		UserOrderVo beanVo = null;
		if (beanDto != null) {
			beanVo = new UserOrderVo();
			BeanUtils.copyProperties(beanDto, beanVo);
			if (OrderTypeEnum.EXCLUSIVEL.equals(beanDto.getOrderType())) {
				// 专属里程订单
				OrderExclusivelStatusEnum orderStatus = (OrderExclusivelStatusEnum) beanDto.getOrderStatus();
				beanVo.setOrderStatus(orderStatus.getValue());
				beanVo.setOrderStatusStr(orderStatus.getName());
			} else if (OrderTypeEnum.SUBSCRIBE.equals(beanDto.getOrderType())) {
				// 订阅里程订单
				OrderSubscribeStatusEnum orderStatus = (OrderSubscribeStatusEnum) beanDto.getOrderStatus();
				beanVo.setOrderStatus(orderStatus.getValue());
				beanVo.setOrderStatusStr(orderStatus.getName());
			}
		}

		return beanVo;
	}

	public static CarOrderVo toCarOrderVo(CarOrderBizDto beanDto) {
		CarOrderVo beanVo = null;
		if (beanDto != null) {
			beanVo = new CarOrderVo();
			BeanUtils.copyProperties(beanDto, beanVo);
			beanVo.setPaymentMethod(beanDto.getPaymentMethod().getValue());
			beanVo.setPaymentMethodStr(beanDto.getPaymentMethod().getName());
			if (OrderTypeEnum.EXCLUSIVEL.equals(beanDto.getOrderType())) {
				// 专属里程订单
				OrderExclusivelStatusEnum orderStatus = (OrderExclusivelStatusEnum) beanDto.getOrderStatus();
				beanVo.setOrderStatus(orderStatus.getValue());
				beanVo.setOrderStatusStr(orderStatus.getName());
			} else if (OrderTypeEnum.SUBSCRIBE.equals(beanDto.getOrderType())) {
				// 订阅里程订单
				OrderSubscribeStatusEnum orderStatus = (OrderSubscribeStatusEnum) beanDto.getOrderStatus();
				beanVo.setOrderStatus(orderStatus.getValue());
				beanVo.setOrderStatusStr(orderStatus.getName());
			}
		}

		return beanVo;
	}

	public static OrderRepaymentVo toOrderRepaymentVo(OrderScheduleCurrentPeriodDto beanDto) {
		OrderRepaymentVo beanVo = null;
		if (beanDto != null) {
			beanVo = new OrderRepaymentVo();
			BeanUtils.copyProperties(beanDto, beanVo);
		}
		return beanVo;
	}

	public static OrderOverdueVo toOrderOverdueVo(OrderScheduleOverdueDto beanDto) {
		OrderOverdueVo beanVo = null;
		if (beanDto != null) {
			beanVo = new OrderOverdueVo();
			BeanUtils.copyProperties(beanDto, beanVo);
		}
		return beanVo;
	}

	public static List<OrderContractVo> toOrderContractVo(List<ContractDto> beanDtoList) {
		List<OrderContractVo> beanVoList = null;
		if (beanDtoList != null && !beanDtoList.isEmpty()) {
			beanVoList = new ArrayList<>();
			for (ContractDto beanDto : beanDtoList) {
				beanVoList.add(toOrderContractVo(beanDto));
			}
		}
		return beanVoList;
	}

	public static OrderContractVo toOrderContractVo(ContractDto beanDto) {
		OrderContractVo beanVo = null;
		if (beanDto != null) {
			beanVo = new OrderContractVo();
			BeanUtils.copyProperties(beanDto, beanVo);
		}

		return beanVo;
	}

	public static OrderAuditVo toOrderAuditVo(OrderAuditDto beanDto) {
		OrderAuditVo beanVo = null;
		if (beanDto != null) {
			beanVo = new OrderAuditVo();
			BeanUtils.copyProperties(beanDto, beanVo);
			beanVo.setResult(beanDto.getResult().getValue());
			beanVo.setResultStr(beanDto.getResult().getName());
		}
		return beanVo;
	}

	public static OrderRenegeVo toOrderRenegeVo(OrderRenegeBizDto dto) {
		OrderRenegeVo vo = null;
		if (dto != null) {
			vo = new OrderRenegeVo();
			BeanUtils.copyProperties(dto, vo);
		}
		return vo;
	}
}
